<template>
  <div>
    <el-form ref="editForm" :model="editForm" label-width="250px" label-position="left">
      <p>数据权限 - 行权限</p>
      <el-row>
        <el-col :span="12" v-for="(item, index) in dataList" :key="index">
          <el-form-item :label="item.authKeyDesc" :prop="item.authKeyDesc">
            <template v-if="editing">
              <el-select
                v-model="item.authValue"
                filterable
                remote
                :remote-method="
                  query => {
                    getTags(query, item)
                  }
                "
                placeholder="请输入标签名称"
              >
                <el-option label="" value="">--</el-option>
                <el-option :label="item.authValueDesc" :value="item.authValue" v-if="isEmpty(item)"></el-option>
                <el-option
                  v-for="tag in item.tagList"
                  :key="tag.assetId"
                  :label="tag.assetNm"
                  :value="tag.assetId"
                ></el-option>
              </el-select>
            </template>
            <template v-else>
              <el-input v-model="item.authValueDesc" disabled></el-input>
            </template>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row class="data-auth-btns">
      <el-col :span="24" align="right">
        <template v-if="!editing">
          <el-button type="primary" @click="editing = true">修改</el-button>
        </template>
        <template v-else>
          <el-button @click="cancelEdit">取消</el-button>
          <el-button type="primary" @click="submit">确定</el-button>
        </template>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import cloneDeep from 'lodash/cloneDeep'
export default {
  data() {
    return {
      editing: false,
      oldList: [],
      dataList: [],
      editForm: {}
    }
  },
  methods: {
    getParams() {
      this.loading = true
      this.$ajax({
        url: this.$ajax.setUrl('ast/astTagDerivedAuth/row/list'),
        method: 'get',
        params: this.$ajax.setParams()
      }).then(({ data }) => {
        if (data && data.code === '0') {
          let list = []
          data.data.forEach((item, index) => {
            list.push(Object.assign({}, item, { tagList: [] }))
            if (!list[index].authValue) list[index].authValue = ''
          })
          this.oldList = cloneDeep(list)
          this.dataList = cloneDeep(list)
        } else {
          this.$message.error(data.data || data.msg)
        }
        this.loading = false
      })
    },
    getTags(query, item) {
      if (query === '') {
        item.tagList = []
      } else {
        this.$ajax({
          url: this.$ajax.setUrl(`ast/astTagDerivedAuth/tagList/${query}`),
          method: 'get',
          params: this.$ajax.setParams()
        }).then(({ data }) => {
          if (data && data.code === '0') {
            item.tagList = data.data
          } else {
            this.$message.error(data.data || data.msg)
          }
        })
      }
    },
    isEmpty(item) {
      if (!item.authValue || !item.tagList) return false
      return !item.tagList.find(tag => tag.assetId === item.authValue)
    },
    cancelEdit() {
      this.editing = false
      this.dataList = cloneDeep(this.oldList)
    },
    submit() {
      this.$ajax({
        url: this.$ajax.setUrl('ast/astTagDerivedAuth/row'),
        method: 'post',
        data: this.$ajax.setData(this.dataList, false, 'json')
      }).then(({ data }) => {
        if (data && data.code === '0') {
          this.$message.success(data.msg)
          this.editing = false
          this.getParams()
        } else {
          this.$message.error(data.data || data.msg)
        }
      })
    }
  }
}
</script>

<style lang="scss"></style>
